/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
        int numsA=0,numsB=0;
        ListNode* mid_A=headA; ListNode* mid_B=headB;
        while(mid_A)
        {
            numsA++;
            mid_A=mid_A->next;
        }
        while(mid_B)
        {
            numsB++;
            mid_B=mid_B->next;
        }
        int k=abs(numsA-numsB);
        ListNode* mid=headA; ListNode* mid_T=headB;
        if(numsB>numsA) 
        {
            mid=headB;
            mid_T=headA;
        }
        while(k--)
        {
            mid=mid->next;
        }
        while(mid)
        {
            if(mid==mid_T) return mid;
            mid=mid->next;
            mid_T=mid_T->next;
        }
        return NULL;
    }
};
